package com.lee.localdate;


import org.junit.Test;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;

public class TestDemo {


    @Test
    public void test1() {
        //今天日期
        LocalDate today = LocalDate.now();
        System.out.println(today);

        //今天几号
        int dayOfMonth = today.getDayOfMonth();
        System.out.println(dayOfMonth);

        //今天周几
        DayOfWeek dayOfWeek = today.getDayOfWeek();
        System.out.println(dayOfWeek);

        //今年是哪一年
        int dayOfYear = today.getDayOfYear();
        System.out.println(dayOfYear);


    }

    @Test
    public void test2() {

        LocalDate today = LocalDate.now();

        //获取本月的第一天
        LocalDate firstDayOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());
        System.out.println(firstDayOfMonth);

        //本月第二填
        LocalDate secondDayOfMonth = today.withDayOfMonth(2);
        System.out.println(secondDayOfMonth);

        //本月最后一天
        LocalDate lastDayOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());
        System.out.println(lastDayOfMonth);

        //当前时间下一天
        LocalDate nextDay = today.plusDays(1);
        System.out.println(nextDay);

    }

    @Test
    public void test3() {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
        DateTimeFormatter formatter2 = DateTimeFormatter.ofPattern("yyyy/MM/dd");
        LocalDate date = LocalDate.parse("20190101", formatter);
        String format = date.format(formatter2);
        System.out.println(format);
    }

    @Test
    public void test4() {

        String beginTime = "2019-01-01";
        String endTime = "2019-02-01";

        for (LocalDate i = LocalDate.parse(beginTime); i.compareTo(LocalDate.parse(endTime)) <= 0; i = i.plusDays(1)) {
            System.out.println(i);
        }

//        LocalDate.parse(beginTime).isBefore()
        LocalDate.parse(beginTime);
    }


}
